package MyThread;

import MyClass.Transaction;
import Tools.Dispatch;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @Author: 0162977
 * @Date: 2020/11/13 12:27
 */
public class TransactionThread implements Runnable{
    private static AtomicBoolean notify = new AtomicBoolean(false);
    private int TNo;
    private List<String> content;

    private volatile boolean suspendFlag ;// 线程挂起的标识

    private Map<Integer,Integer> privateBuf = new HashMap<Integer, Integer>();
    public TransactionThread(int tNo){
        this.TNo = tNo;
        content = Dispatch.getTransactionByNo(TNo).getContent();
    }
    @Override
    public void run() {
        suspendFlag = false ;
        for(int i= 0 ;i<content.size();i++){
            while(suspendFlag){
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            while (!notify.compareAndSet(false,true)){;}

            String temp = content.get(i);
            String op = temp.split(",")[0];
            if(op.equals("S") ){
                System.out.println("["+String.valueOf(TNo)+"]" + " " +"Strat");
                Dispatch.addStartTime(TNo,System.currentTimeMillis());
            }else if (op.equals("V")){
                System.out.println("["+String.valueOf(TNo)+"]" + " " +"V");
                int VFlag = Dispatch.addVerificationTime(TNo,System.currentTimeMillis());
                System.out.println(VFlag);
                if(VFlag == 0){
                    Dispatch.deleteTranTime(TNo);
                    Dispatch.openThread(TNo);
                    notify.set(false);
                    break;
                }else if(VFlag == 2){
                    i = i-1;
                    suspendFlag = true;
                }
            }else if (op.equals("F")){
                System.out.println("["+String.valueOf(TNo)+"]" + " " +"F");
                Dispatch.addFinishTime(TNo,System.currentTimeMillis());
            }else if (op.equals("0")){ //读操作

                System.out.println("["+String.valueOf(TNo)+"]" + " " +"Read");
                try {
                    privateBuf.put(Integer.parseInt(temp.split(",")[1]),Dispatch.readMemory(Integer.parseInt(temp.split(",")[1])));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else if (op.equals("1")){ //写操作
                System.out.println("["+String.valueOf(TNo)+"]" + " " +"Write");
                try {
                    Dispatch.writeMemory(Integer.parseInt(temp.split(",")[1]),privateBuf.get(Integer.parseInt(temp.split(",")[1])));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else if (op.equals("2")){ //加法操作
                System.out.println("["+String.valueOf(TNo)+"]" + " " +"Add");
                int data1 = privateBuf.get(Integer.parseInt(temp.split(",")[1]));
                int data2 = privateBuf.get(Integer.parseInt(temp.split(",")[2]));
                data1 = data2 + Integer.parseInt(temp.split(",")[3]);
                privateBuf.put(Integer.parseInt(temp.split(",")[1]),data1);
            }else if (op.equals("3")){ //乘法操作
                System.out.println("["+String.valueOf(TNo)+"]" + " " +"Multi");
                int data1 = privateBuf.get(Integer.parseInt(temp.split(",")[1]));
                int data2 = privateBuf.get(Integer.parseInt(temp.split(",")[2]));
                data1 = data2 * Integer.parseInt(temp.split(",")[3]);
                privateBuf.put(Integer.parseInt(temp.split(",")[1]),data1);
            }
//            System.out.println("\t\t\t["+String.valueOf(TNo)+"]" + " " +temp);

            notify.set(false);
        }
    }
    public void resume(){
        suspendFlag = false;
    }

}